package com.star.modules.sys.api;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.mapper.BeanMapper;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.utils.DictUtils;
import com.star.modules.sys.base.BizDataResult;
import com.star.modules.sys.enums.DictEnum;
import com.star.modules.sys.enums.InfoTypeEnum;
import com.star.modules.sys.enums.ResultEnum;
import com.star.modules.sys.excetion.ApiException;
import com.star.modules.sys.service.IndexService;
import com.star.modules.sys.vo.DictVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 首页接口类
 */
@RestController
@RequestMapping(value = "${frontPath}/sys/index")
public class IndexController {

    @Autowired
    private IndexService indexService;

    /**
     * 首页展示信息
     *
     * @return
     */
    @RequestMapping(value = "info")
    public BizDataResult getIndexInfo() {
        return indexService.getIndexInfo();
    }

    /**
     * 字典下拉
     *
     * @return
     */
    @RequestMapping(value = "menuCol")
    public BizDataResult menuCol() {
        Map<String, List<DictVo>> result = MapUtils.newHashMap();
        try {
            List<DictData> dictDataList = DictUtils.getDictList(DictEnum.INFO_TYPE.getCode());
            List<DictData> informationList = ListUtils.newArrayList();
            List<DictData> newsList = ListUtils.newArrayList();
            List<DictData> brokeTeamList = ListUtils.newArrayList();
            for (DictData dict : dictDataList) {
                if (dict.getDictValue().equals(String.valueOf(InfoTypeEnum.INFOMATION.getCode()))) {
                    for (DictData dictData : dictDataList) {
                        if (dictData.getParentCode().equals(dict.getId())) {
                            informationList.add(dictData);
                        }
                    }
                }
                if (dict.getDictValue().equals(String.valueOf(InfoTypeEnum.NEWS.getCode()))) {
                    for (DictData dictData : dictDataList) {
                        if (dictData.getParentCode().equals(dict.getId())) {
                            newsList.add(dictData);
                        }
                    }
                }
                if (dict.getDictValue().equals(String.valueOf(InfoTypeEnum.BROKETEAM.getCode()))) {
                    for (DictData dictData : dictDataList) {
                        if (dictData.getParentCode().equals(dict.getId())) {
                            brokeTeamList.add(dictData);
                        }
                    }
                }
            }
            result.put("inforMation",BeanMapper.mapList(informationList,DictVo.class));
            result.put("news",BeanMapper.mapList(newsList,DictVo.class));
            result.put("brokeTeam",BeanMapper.mapList(brokeTeamList,DictVo.class));
        }catch (Exception e) {
            throw new ApiException(e);
        }
        return BizDataResult.createResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), result);
    }
}
